package com.hqyj.javaGraduation.hotel.service.Impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hqyj.javaGraduation.account.dao.UserDao;
import com.hqyj.javaGraduation.account.pojo.User;
import com.hqyj.javaGraduation.config.PayConfig;
import com.hqyj.javaGraduation.hotel.dao.UtMenuInfoDao;
import com.hqyj.javaGraduation.hotel.pojo.UtMenuInfo;
import com.hqyj.javaGraduation.hotel.service.AliPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class AliPayServiceImpl implements AliPayService {

    //支付配置
    @Autowired
    AlipayClient alipayClient;

    //读取payConfig.文件的注入
    @Autowired
    PayConfig payConfig;

    @Autowired
    UtMenuInfoDao utMenuInfoDao;

    @Autowired
    UserDao userDao;

    @Override
    public String aliPay(UtMenuInfo utMenuInfo) {

        HashMap<String,Object> map = new HashMap<>();
        //订单编号
        map.put("out_trade_no",""+utMenuInfo.getTableId());
        //商品状态: 1 支付 2 未支付
        map.put("product_code", "FAST_INSTANT_TRADE_PAY");
        double sum = 0;
        List<UtMenuInfo> utMenuInfos = utMenuInfoDao.queryAllPrice(utMenuInfo);
        for (UtMenuInfo menuInfo : utMenuInfos) {
            sum+=menuInfo.getMenuNum()*menuInfo.getMenuPrice();
        }
        User user = userDao.queryUserMember(utMenuInfo);
        if(user.getUserMember().equals("黄金会员")){
            sum=sum*0.9;
        }
        if(user.getUserMember().equals("砖石会员")){
            sum=sum*0.8;
        }
        //付款金额
        map.put("total_amount",sum);
        //商品名称
        map.put("subject","商品消费");
        //商品描述
        map.put("body","商品消费");

        try {
            //把请求参数转换成json字符串格式
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonParams =objectMapper.writeValueAsString(map);

            // 设置阿里表单支付请求对象
            AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
            //设置参数
            alipayRequest.setBizContent(jsonParams);
            //设置支付成功返回的回调地址
            alipayRequest.setReturnUrl(payConfig.getReturnUrl());
            //设置异步回调的地址
            alipayRequest.setNotifyUrl(payConfig.getNotifyUrl());
            //返回阿里的支付页面
            try {
                System.out.println(alipayClient.pageExecute(alipayRequest).getBody());
                return alipayClient.pageExecute(alipayRequest).getBody();
            } catch (AlipayApiException e) {
                e.printStackTrace();
            }

        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public HashMap<String, Object> aliPay02(UtMenuInfo utMenuInfo) {
        HashMap<String, Object> mmm = new HashMap<String, Object>();

        HashMap<String,Object> map = new HashMap<>();
        //订单编号
        map.put("out_trade_no",utMenuInfo.getTableId()+""+new Date());

        double sum = 0;
        List<UtMenuInfo> utMenuInfos = utMenuInfoDao.queryAllPrice(utMenuInfo);
        for (UtMenuInfo menuInfo : utMenuInfos) {
            sum+=menuInfo.getMenuNum()*menuInfo.getMenuPrice();
        }
/*        List<UtMenuInfo> utMenuInfos = utMenuInfoDao.queryAllPrice(utMenuInfo);
        for (UtMenuInfo menuInfo : utMenuInfos) {
            sum+=menuInfo.getMenuNum()*menuInfo.getMenuPrice();
        }
        User user = userDao.queryUserMember(utMenuInfo);
        if(user.getUserMember().equals("黄金会员")){
            sum=sum*0.9;
        }
        if(user.getUserMember().equals("砖石会员")){
            sum=sum*0.8;
        }*/
        //付款金额
        map.put("total_amount",sum);
        //商品名称
        map.put("subject","商品消费");
        //商品描述
        map.put("body","商品消费");

        try {
            //把请求参数转换成json字符串格式
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonParams = objectMapper.writeValueAsString(map);

            // 设置阿里表单支付请求对象
            AlipayTradePrecreateRequest alipayRequest = new AlipayTradePrecreateRequest();
            //设置参数
            alipayRequest.setBizContent(jsonParams);
            //设置支付成功返回的回调地址
            alipayRequest.setReturnUrl(payConfig.getReturnUrl());
            //设置异步回调的地址
            alipayRequest.setNotifyUrl(payConfig.getNotifyUrl());

            //获取支付宝支付响应对象
            AlipayTradePrecreateResponse alipayResponse = alipayClient.execute(alipayRequest);
            System.out.println(alipayResponse.toString());

            if(alipayResponse.isSuccess()){
                System.out.println("二维码支付地址="+alipayResponse.getQrCode());
                utMenuInfoDao.updateUtMenuInfoState(utMenuInfo);
                mmm.put("url",alipayResponse.getQrCode());
            }else{
                mmm.put("url","error");
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return mmm;
    }
}
